<?php
/**
 * 管理中心· 商品模块的逻辑处理
 * ============================================================================
 * 版权所有 2017-2020 北京优特世纪科技有限公司，并保留所有权利。
 * 网站地址: http://www.unitewww.com;
 * ============================================================================
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * @since: 1.0
 * @version: 1.0
 **/
namespace Hoteladmin\Model;
use Think\Model;
class GoodsModel extends Model{
    /**
     +--------------------------------------------------------------------------
     * 实现添加或修改商品信息
     * @author:  XC跃
     * @date: 2018-09-29
     +--------------------------------------------------------------------------
     */
    public function goods_add_edit($id){
	//实例化相关表
	$goident = M('goods');
	$caident = M('category');
	$roident = M('rtype');
	$data = $goident->create();
	//判断商品分类
//        echo "<pre>";
//        print_r($_POST['pro']);
//        print_r($_POST['city']);
//        print_r($_POST['area']);
//        print_r($_POST  ['specific']);
//        die;
	$caid = $_POST['cat_id'];
	$roid = $_POST['ro_id'];
	$coun = $caident->where("`ca_id`='".$caid."' and `ca_parent`='0'")->count();
//	$roun = $roident->where("`ro_rid`='".$roid)->count();
//	if($coun>0){
//		$res = array('status'=>-1,'msg'=>'请选择当前分类的子分类');
//		exit(json_encode($res));
//	}
	$data['go_caid'] = $caid;
	$data['go_rid'] = $roid;
	$data['go_thumb']= $_POST['thumbImgUrl'];
	$data['go_body'] = $_POST['body'];
	$data['go_pro'] = $_POST['pro'];
	$data['go_shi'] = $_POST['city'];
	$data['go_area'] = $_POST['area'];
	$data['go_specific'] = $_POST['specific'];
	$a = $_POST['go_type1'];
	$b = $_POST['go_type2'];
	$date['go_type'] = $a.",".$b;
	if(''!=$id){
		$re = $goident->where("`go_id`='".$id."'")->save($data);
		$this->good_img_sn($id);
	}else{
  		$data['go_type'] = '1';
		$re = $goident->add($data);
		$this->good_img_sn($re);
	}
	$re>0?$res=1:$res=0;
	return $res;
}

	public function points_add_edit($id){
		//实例化相关表
		$goident = M('goods');
		$caident = M('category');
		$data = $goident->create();
		//判断商品分类
		$caid = $_POST['cat_id'];;
		$coun = $caident->where("`ca_id`='".$caid."' and `ca_parent`='0'")->count();
		if($coun>0){
			$res = array('status'=>-1,'msg'=>'请选择当前分类的子分类');
			exit(json_encode($res));
		}
		$data['go_caid'] = $caid;
		$data['go_thumb']= $_POST['thumbImgUrl'];
		$data['go_body'] = $_POST['body'];
		if(''!=$id){
			$data['go_utime'] = time();
			$re = $goident->where("`go_id`='".$id."'")->save($data);
			$this->good_img_sn($id);
		}else{
			$data['go_stime'] = time();
			$data['go_type'] = '2';
			$re = $goident->add($data);
			$this->good_img_sn($re);
		}
		$re>0?$res=1:$res=0;
		return $res;
	}
    //商品相册及货号
    public function daili_img_sn($goid){
        //实例化相关表
        $giident = M('daili_images');
        // 商品图片相册不为空
        $count = count($_POST['pc_src']);//获取传过来有几张图片
        //dump($_POST['pc_src']);die;
        if($count){
            for($i=0;$i<$count;$i++){
                $img = $_POST['pc_src'][$i];
                $giFind = $giident->where("`im_gid`='".$goid."' and `im_url`='".$img."'")->find();
                if(count($giFind)=='0'){
                    $data['im_gid'] = $goid;
                    $data['im_url'] = $img;
                    $data['im_type']= 1;
                    $giident->add($data);
                }else{
                    $data['im_sort']= $i;
                    $giident->where("`im_url`='".$img."'")->save($data);
                }
            }
        }
        //清除缓存图片
        $dir="./Public/Uploads/thumb/".$goid;
        clearCache($dir);
    }
    //商品相册及货号
    public function good_img_sn($goid){
        //实例化相关表
        $goident = M('goods');
        $giident = M('goods_images');
        //商品货号
        $go_sn = "XC".str_pad($goid,7,"0",STR_PAD_LEFT);
        $goident->where("`go_id`='".$goid."'")->save(array('go_sn'=>$go_sn));
        // 商品图片相册不为空
        $count = count($_POST['pc_src']);//获取传过来有几张图片
        //dump($_POST['pc_src']);die;
        if($count){
            for($i=0;$i<$count;$i++){
                $img = $_POST['pc_src'][$i];
                $giFind = $giident->where("`im_gid`='".$goid."' and `im_url`='".$img."'")->find();
                if(count($giFind)=='0'){
                    $data['im_gid'] = $goid;
                    $data['im_url'] = $img;
                    $data['im_type']= 1;
                    $giident->add($data);
                }else{
                    $data['im_sort']= $i;
                    $giident->where("`im_url`='".$img."'")->save($data);
                }
            }
        }
        //清除缓存图片
        $dir="./Public/Uploads/thumb/".$goid;
        clearCache($dir);
    }
    /**
     +--------------------------------------------------------------------------
     * 实现获取全部顶级分类
     * @author:XC跃
     * @Date:2018-10-10
     +--------------------------------------------------------------------------
     */
    public function getCateParent() {
        //实例化相关表
        $caident = M('category');
        $caList = $caident->where("`ca_parent`='0'")->field('ca_id,ca_name')->select();
        //dump($caList);
        return $caList;
    }
    /**
     +--------------------------------------------------------------------------
     * 实现获取指定ID分类
     * @author:XC跃
     * @Date:2018-10-10
     +--------------------------------------------------------------------------
     */
    public function getCateFind($cid){
        //实例化相关表
        $caident = M('category');
        $caFind = $caident->where("`ca_id`='".$cid."'")->find();
        //dump($caFind);
        return $caFind;
    }
    /**
     +--------------------------------------------------------------------------
     * 实现添加(编辑)商品分类的功能
     * @author:XC跃
     * @Date:2018-10-10
     +--------------------------------------------------------------------------
     */
    public function cate_add_edit($cid){
        //实例化相关表
        $caident = M('category');
        $data['ca_name']  = trim(I('post.cname'));
        $data['ca_parent']= trim(I('post.level'));
        //$data['ca_brid']  = implode(',', $_POST['brid']);
        $data['ca_thumb'] = trim(I('post.adImgUrl'));
        $data['ca_sort']  = trim(I('post.sort'));
    
        if($cid>0){
            $res = $caident->where("`ca_id`='".$cid."'")->save($data);
        }else{
            $res = $caident->add($data);
        }
        return $res;
    }

    /**
    +--------------------------------------------------------------------------
     * 实现获取房间分类
     *
    +--------------------------------------------------------------------------
     */
    public function getRoomParent() {
        //实例化相关表
        $roident = M('rtype');
        $roList = $roident->select();
        //dump($caList);
        return $roList;
    }

    /**
    +--------------------------------------------------------------------------
     * 实现获取指定ID分类
     * @author:XC跃
     * @Date:2018-10-10
    +--------------------------------------------------------------------------
     */
    public function getRoomFind($rid){
        //实例化相关表
        $roident = M('rtype');
        $roFind = $roident->where("`ro_rid`='".$rid."'")->find();
        //dump($caFind);
        return $roFind;
    }
    /**
    +--------------------------------------------------------------------------
     * 实现添加(编辑)房间分类的功能
     * @author:XC跃
     * @Date:2018-10-10
    +--------------------------------------------------------------------------
     */
    public function room_add_edit($rid){
        //实例化相关表
        $roident = M('rtype');
        $data['ro_room']  = trim(I('post.rname'));

        if($rid>0){
            $res = $roident->where("`ro_rid`='".$rid."'")->save($data);
        }else{
            $res = $roident->add($data);
        }
        return $res;
    }

}